


#ifndef PF_SPLINE_H
#define PF_SPLINE_H

#include <QList>
#include "pf_entitycontainer.h"
#include "lx_Export.h"

/**
 * Holds the data that defines a line.
 */
class LX_EXPORT PF_SplineAttribute {
public:
    /**
     * Default constructor. Leaves the data object uninitialized.
     */
    PF_SplineAttribute() {}
    
	PF_SplineAttribute(int degree, bool closed) {
		this->degree = degree;
		this->closed = closed;
	}

    friend std::ostream& operator << (std::ostream& os, const PF_SplineAttribute& ld) {
        os << "( degree: " << ld.degree <<
        " closed: " << ld.closed <<
        ")";
        return os;
    }

public:
	/** Degree of the spline (1, 2, 3) */
	int degree;
	/** Closed flag. */
	bool closed;
	/** Control points of the spline. */
    QList<PF_Vector> controlPoints;
};


/**
 * Class for a spline entity.
 *

 */
class LX_EXPORT PF_Spline : public PF_Container {
public:
    PF_Spline(PF_Container* parent,
            const PF_SplineAttribute& d);

    virtual PF_Entity* clone();

    virtual ~PF_Spline();

    /**	@return LX_Define::EntitySpline */
    virtual LX_Define::EntityType rtti() const {
        return LX_Define::EntitySpline;
    }
    /** @return false */
    virtual bool isEdge() const {
        return false;
    }

    /** @return Copy of data that defines the spline. */
    PF_SplineAttribute getData() const {
        return data;
    }
	
	/** Sets the splines degree (1-3). */
	void setDegree(int deg) {
		if (deg>=1 && deg<=3) {
			data.degree = deg;
		}
	}

	/** @return Degree of this spline curve (1-3).*/
	int getDegree() {
		return data.degree;
	}

	/** @return 0. */
    int getNumberOfKnots() {
		return 0;
	}

	/** @return Number of control points. */
	int getNumberOfControlPoints() {
		return data.controlPoints.count();
	}

	/** 
	 * @retval true if the spline is closed.
	 * @retval false otherwise.
	 */
	bool isClosed() {
		return data.closed;
	}

	/**
	 * Sets the closed falg of this spline.
	 */
	void setClosed(bool c) {
		data.closed = c;
		update();
	}

    virtual PF_VectorSolutions getRefPoints();
    virtual PF_Vector getNearestRef(const PF_Vector& coord,
                                     double* dist = NULL);
    virtual PF_Vector getNearestSelectedRef(const PF_Vector& coord,
                                     double* dist = NULL);

    /** @return Start point of the entity */
    //virtual PF_Vector getStartpoint() const {
    //    return data.startpoint;
    //}
    /** @return End point of the entity */
    //virtual PF_Vector getEndpoint() const {
    //    return data.endpoint;
    //}
    /** Sets the startpoint */
    //void setStartpoint(PF_Vector s) {
    //    data.startpoint = s;
    //    calculateBorders();
    //}
    /** Sets the endpoint */
    //void setEndpoint(PF_Vector e) {
    //    data.endpoint = e;
    //    calculateBorders();
    //}

	void update();
	
	//virtual void moveStartpoint(const PF_Vector& pos);
	//virtual void moveEndpoint(const PF_Vector& pos);
	//virtual LX_Define::Ending getTrimPoint(const PF_Vector& coord, 
	//          const PF_Vector& trimPoint);
	//virtual void reverse();
    /** @return the center point of the line. */
    //PF_Vector getMiddlepoint() {
    //    return (data.startpoint + data.endpoint)/2.0;
    //}
	//virtual bool hasEndpointsWithinWindow(PF_Vector v1, PF_Vector v2);

    /**
     * @return The length of the line.
     */
    //virtual double getLength() {
    //    return data.startpoint.distanceTo(data.endpoint);
    //}

    /**
     * @return The angle of the line (from start to endpoint).
     */
    //virtual double getAngle1() {
    //    return data.startpoint.angleTo(data.endpoint);
    //}

    /**
     * @return The angle of the line (from end to startpoint).
     */
    //virtual double getAngle2() {
    //    return data.endpoint.angleTo(data.startpoint);
    //}

    virtual PF_Vector getNearestEndpoint(const PF_Vector& coord,
                                         double* dist = NULL);
    //virtual PF_Vector getNearestPointOnEntity(const PF_Vector& coord,
    //        bool onEntity=true, double* dist = NULL, PF_Entity** entity=NULL);
    virtual PF_Vector getNearestCenter(const PF_Vector& coord,
                                       double* dist = NULL);
    virtual PF_Vector getNearestMiddle(const PF_Vector& coord,
                                       double* dist = NULL);
    virtual PF_Vector getNearestDist(double distance,
                                     const PF_Vector& coord,
                                     double* dist = NULL);
    //virtual PF_Vector getNearestRef(const PF_Vector& coord,
    //                                 double* dist = NULL);
    /*virtual double getDistanceToPoint(const PF_Vector& coord,
                                      PF_Entity** entity=NULL,
                                      LX_Define::ResolveLevel level=LX_Define::ResolveNone,
									  double solidDist = PF_MAXDOUBLE);*/
    
	virtual void addControlPoint(const PF_Vector& v);
	virtual void removeLastControlPoint();

    virtual void move(PF_Vector offset);
    virtual void rotate(PF_Vector center, double angle);
    virtual void scale(PF_Vector center, PF_Vector factor);
    virtual void mirror(PF_Vector axisPoint1, PF_Vector axisPoint2);
	virtual void moveRef(const PF_Vector& ref, const PF_Vector& offset);

    virtual void draw(PF_Painter* painter, PF_GraphicView* view, double patternOffset=0.0);
        QList<PF_Vector> getControlPoints();

    friend std::ostream& operator << (std::ostream& os, const PF_Spline& l);

    virtual void calculateBorders();

	static void rbasis(int c, double t, int npts, int x[], double h[], double r[]);
	
	static void knot(int num, int order, int knotVector[]);
	static void rbspline(int npts, int k, int p1,
		double b[], double h[], double p[]);
		
	static void knotu(int num, int order, int knotVector[]);
	static void rbsplinu(int npts, int k, int p1,
		double b[], double h[], double p[]);

protected:
    PF_SplineAttribute data;
}
;

#endif
